<?php
    /* functions */
    function serviceAiLaTrieuPhu($data, $syntax)
    {
        $client = new SoapClient("http://yocity.sunkhoai.com:8080/yocityws/yocityws?wsdl");
                                     
        $info = array();
        $info["username"] = "taoviec"; 
        $info["password"] = "@#taoviec2012"; 
        $info["serverip"] = "0"; 
        $info["fullcontent"] = "YO" . $syntax["code"] . " " . $syntax["username"] . " " . $syntax["serverId"] . " " . $data["price"] . " " . $data["sender"]; 
        $info["servicecode"] = $data["serviceCode"]; 
        
        $result = $client->__call("yocityws", array($info));
        return $result;
    }
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $code = !empty($match[1]) ? $match[1] : "";        
        $username = !empty($match[3]) ? $match[3] : 0;        
        $refCode = !empty($match[4]) ? $match[4] : "";        
        $serverId = !empty($match[6]) ? $match[6] : 0;        
                
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;
        $result["serverId"] = $serverId;
        $result["code"] = $code;
        
        return $result;
    }
    
    function serviceYoyo($data, $syntax)
    {        
        $url = "http://chat.sunkhoai.com:8888/cp/index.php";
        
        $url .= "?ym=" . $syntax["username"];
        $url .= "&phone=" . $data["sender"];
        $url .= "&cp=taoviec";
        $url .= "&pass=taoviec201210120204";
        
        $result = cUrl($url);        
        $result = trim($result);
        $result = explode(":", $result);
        return $result;
    }
    
    function serviceTab($data, $syntax)
    {        
        $url = "http://ws.sunkhoai.com:8080/sunnetmo/services/sunnetmo/values";        
        $url .= "?cpName=taoviec";
        $url .= "&cpPass=taoviec25102012";
        $url .= "&serviceNumber=" . $data["serviceCode"];
        $url .= "&serviceCode=STAB";        
        $url .= "&fullContent=" . $data["content"];        
        $url .= "&phoneNumber=" . $data["sender"];        
        $url .= "&requestTime=" . time();        
        $url .= "&requestId=" . $data["transactionId"];        
        
        $xml  = @simplexml_load_file($url);          
        $xml->registerXPathNamespace('ns', '');
        $result = $xml->xpath('//ns:valuesResponse/ns:return');
        
        $result = (string)$result[0];
        $result = explode("|", $result);
        
        return $result;
    }
    
    function buildSyntaxYoyo($content, $regx)
    {
        preg_match($regx, $content, $match);                
        $username = !empty($match[2]) ? $match[2] : "";        
        $refCode = !empty($match[3]) ? $match[3] : "";                
                
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;
        
        return $result;
    }
    
    function buildSyntaxTab($content, $regx)
    {
        preg_match($regx, $content, $match);                
        $game_name = !empty($match[2]) ? $match[2] : "";        
        $refCode = !empty($match[4]) ? $match[4] : "";                
                
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["game_name"] = $game_name;
        
        return $result;
    } 
    
    /*
    * processing
    */
    
    function processing($data)
    {        
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 56; //ID partner sunnet    
        $data["type"] = 1; //Thể loại game
        
        $regx_nap_trieuphu = "/^YO(.*)(\s+)([a-zA-Z0-9_.]{1,})@([a-zA-Z0-9_]{1,})(\s+)(\d+)$/si";               
        $regx_yoyo = "/^TVCH(\s+)([a-zA-Z0-9_.]{1,})@([a-zA-Z0-9_]{1,})$/si";               
        $regx_try_and_buy = "/^STAB(\s+)([a-zA-Z0-9]{1,})(\s+)([a-zA-Z0-9]{1,})$/si";                               
        
        $data_reg = array();
        if(
            preg_match($regx_nap_trieuphu, $data["content"])             
            || preg_match($regx_yoyo, $data["content"])             
            || preg_match($regx_try_and_buy, $data["content"])             
        ){
            
            if(preg_match($regx_nap_trieuphu, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_nap_trieuphu);
                $list_service_code = array("6522", "6622", "6722");
                $data["id"] = 152; //ID game
                
            } else if(preg_match($regx_yoyo, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntaxYoyo($data["content"], $regx_yoyo);
                $list_service_code = array("6722");
                $data["id"] = 156; //ID game
                
            } else if(preg_match($regx_try_and_buy, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntaxTab($data["content"], $regx_try_and_buy);
                $list_service_code = array("6522", "6622", "6722");
                switch($syntax["game_name"]){
                    case "ZOMBIE":
                        $data["id"] = 161;
                        break;
                    case "DORAEMON":
                        $data["id"] = 164;
                        break;
                    default:
                        $data["id"] = 0;
                        break;
                }                
            } 
            
            $data_reg["game_user"] = isset($syntax["username"]) ? $syntax["username"] : "";
            $data_reg["username"] = isset($syntax["user_name"]) ? $syntax["user_name"] : "";
            
            $data["userId"] = $syntax["userId"];
            if(in_array($data["serviceCode"], $list_service_code)){                             
                
                if(preg_match($regx_nap_trieuphu, $data["content"])){
                
                    $result = serviceAiLaTrieuPhu($data, $syntax);                        
                     
                    $data["status"] = $result->return==1 ? 1 : 0;
                    $response["status"] = $data["status"];
                    $response["msg"] = $result->return==1 ? "Ban da nap tien thanh cong cho tai khoan " . $syntax["username"] : "Nap tien that bai";
                    
                } else if(preg_match($regx_yoyo, $data["content"])){
                    
                    $result = serviceYoyo($data, $syntax);                        
                     
                    $data["status"] = $result[0]==0 ? 1 : 0;
                    $response["status"] = $data["status"];
                    $response["msg"] = $result[0]==0 ? "Ban da nap xu thanh cong cho tai khoan " . $syntax["username"] : "Nap tien that bai";
                    
                } else if(preg_match($regx_try_and_buy, $data["content"])){
                    
                    $result = serviceTab($data, $syntax);                        
                    
                    $data["status"] = $result[0]==1 ? 1 : 0;
                    $response["status"] = $data["status"];
                    $response["msg"] = $result[0]==1 ? "Ban da giao dich thanh cong. Chuc ban choi vui ve" : "Giao dich that bai";
                    
                }
                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
        }   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        $data["action"] = "reg"; //action
        
        $result = InsertSmsLog($data);   
        
        if(in_array($data["id"], array(152, 156))){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];                
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }            
        }
        
        return $response;
    }
?>
